<template>
	<view class="share_profits">
				<view class="scroll-view-mater" v-if="imageUrl!=''">
					<view class="eat-row s-wrap">
						<view class="s-wrap tkrates-body">
							<text class="tkrates-title">素材图片</text>
							<!-- <text v-if="isquanxuan==0" class="tkrates-copy" @click="quanxuan()">全选</text>
							<text v-if="isquanxuan==1" class="tkrates-copy" @click="quxiao()">取消</text> -->
						</view>
						<!--<view class="left-poster" v-if="imageUrl != ''">
							<image :src="imageUrl" mode="widthFix" @click="previewImage" class="imageUrl"></image>
						</view>-->
						<scroll-view :scroll-x="true" class="eat-small s-wrap" :show-scrollbar="false">
							<!-- <view class="eat-ok">
								<image class="eat-small-img" mode="widthFix" :src="imageUrl"></image>
								<image class="sel-img" src="/static/img/goods/check_icon_img.png"
									style="right: 10rpx; top: 10rpx"></image>
							</view> -->
							<view @tap.stop="catchTapAllPic(item, index)" class="eat-ok"
								v-for="(item, index) in detail.files" :key="index">
								<image class="eat-small-img" mode="widthFix" :src="item.url"></image>
								<image class="sel-img"
									:src="item.isSelectTrue == true ? '/static/img/goods/check_icon_img.png' : '/static/img/goods/uncheck_icon.png'"
									style="right: 10rpx; top: 10rpx"></image>
							</view>
						</scroll-view>
					</view>
				</view>

				<!-- <scroll-view class="scroll-help-view" :scrollY="true" > -->
					<view class="matter-else-text" v-if="detail.content">
						<view class="s-wrap tkrates-body">
							<text class="tkrates-title">素材文案</text>
							<text class="tkrates-copy" @click="copyWord()">复制文案</text>
						</view>
						<text :selectable="true" class="matter-else-text-wenan">{{ detail.content }}</text>
					</view>
				<!-- </scroll-view> -->
			
			<!-- <cell v-if="goodsinfo.goods_info">
				
				<scroll-view class="scroll-help-view" :scrollY="true">
					<view class="matter-else-text">
						<view class="s-wrap tkrates-body">
							<text class="tkrates-title">推荐语</text>
							<text class="tkrates-copy" @click="copyTjy()">复制推荐语</text>
						</view>
						<text :selectable="true" class="matter-else-text-wenan">{{ goodsinfo.goods_info }}</text>
					</view>
				</scroll-view>
			</cell> -->
		
	<view  style="width: 100%;bottom: 0px;position: fixed;z-index: 10000;padding: 14px 0;" v-if="imageUrl!=''">
		<view style="width: 33%;float: left;text-align: center;" @tap="shareWeixin">
			<image src="/static/img/fenxiang/wxshare.png" style="width: 30px;height: 30px;"></image>
			<text style="display: block;text-align: center;">微信好友</text>
		</view>
		<view style="width: 33%;float: left;text-align: center;" @tap="sharetimeline">
			<image src="/static/img/fenxiang/timeline.png" style="width: 30px;height: 30px;"></image>
			<text style="display: block;text-align: center;">朋友圈</text>
		</view>
		<view style="width: 33%;float: left;text-align: center;" @tap="executeDownload(0)">
			<image src="/static/img/fenxiang/save_into_circle.png" style="width: 30px;height: 30px;"></image>
			<text style="display: block;text-align: center;">保存到相册</text>
		</view>
		
	</view>
		<!-- <view class="new-share-view">
			<view class="bottom-share-view s-nowrap s-space-between">
				<view class="new-share-item" @tap="shareWeixin">
					<image class="new-share-item-image" src="/static/img/fenxiang/group_or_friend.png"></image>
					<text class="new-share-item-text">微信好友</text>
				</view>
				<view class="new-share-item" @tap="sharetimeline">
					<image class="new-share-item-image" src="/static/img/fenxiang/group.png"></image>
					<text class="new-share-item-text">朋友圈</text>
				</view>

				<view class="new-share-item" @tap="executeDownload(0)">
					<image class="new-share-item-image" src="/static/img/fenxiang/save_into_circle.png">
					</image>
					<text class="new-share-item-text">保存到相册</text>
				</view>
			</view>
		</view> -->
	</view>
</template>

<script>
	import helper from '@/common/helper.js';
	//import share from '@/common/app-plus/simple-share.js';
	export default {
		data() {
			return {
				userInfo: {},
				goodsid: '',
				goodsinfo: {},
				spinChain: {},
				otherImportImage: [],
				imageUrl: '',
				isquanxuan: 0,
				//new
				id:'',
				detail:{}
				
				
			};
		},
		onLoad(e) {
			this.userInfo = this.$store.state.userinfo;
			//this.goodsid = e.itemid;
			this.id = e.itemid;
			this.getInfo(e.itemid)
			//var vm = helper.currPage(2);
			//var spinChain = vm.spinChain;
			//console.log(vm.detail);
			///if (spinChain) {
			//	this.spinChain = spinChain;
			//	this.onLoadGoods(vm.detail);
			//} else {
			// 	uni.showToast({
			// 		title: '获取商品信息失败',
			// 		icon: 'none'
			// 	});
			// 	setTimeout(r => {
			// 		uni.navigateBack();
			// 	}, 800);
			// }
		},
		methods: {
			getInfo(id){
				console.log(id);
				this.$http.request({
					url: '/topic/sucaiInfo?topicId='+id,
					success: (res) => {
						if (res.data.code == 200) {
							console.log(res.data);
							//var item=res.data.data
							this.detail=res.data.data;
							if(res.data.data.files){
								this.imageUrl =res.data.data.files;
							}
							
							console.log(this.detail);
							//if(this.detail.canDeleted){
								//this.showtitleNViewBtns=true
								///this.$fc.setTitleNViewBtns(0,'\ue623')
							//}
						}
					}
				});
			},
			test_weixin_share_mul_pic(pics) {
				var Intent = plus.android.importClass('android.content.Intent');
				var ComponentName = plus.android.importClass('android.content.ComponentName');
				var ArrayList = plus.android.importClass('java.util.ArrayList');
				var Uri = plus.android.importClass('android.net.Uri');
				var Environment = plus.android.importClass('android.os.Environment');
				var File = plus.android.importClass('java.io.File');
				var sdcardDir = plus.android.invoke(Environment.getExternalStorageDirectory(), 'getAbsolutePath');
				var intent = new Intent();
				var localComponentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
				intent.setComponent(localComponentName);
				intent.setAction("android.intent.action.SEND_MULTIPLE");
				intent.setType("image/*");
				var localArrayList = new ArrayList();
				//var pics = ['1.png','2.jpg','3.jpg','4.jpg'];
				for (var i = 0; i < pics.length; i++) {
					var filePath = pics[i];
					console.log('filePath=' + filePath);
					localArrayList.add(Uri.fromFile(new File(filePath)));
				}
				intent.putParcelableArrayListExtra("android.intent.extra.STREAM", localArrayList);
				var act = plus.android.runtimeMainActivity();
				act.startActivity(intent);
			},
			shareWeixin() {
				this.executeDownload(1);
				//console.log(788877);
			},
			executeDownload(type) {
				//console.log(666);
				var that =this;
				var selectbdList = [];
				var selectList = [];
				//保存其他图片
				var otherImportImage = this.imageUrl;
				for (var i = 0; i < otherImportImage.length; i++) {
					console.log(otherImportImage[i].url);
					selectList.push(otherImportImage[i].url);
					// if (otherImportImage[i]['isSelectTrue']) {
					// 	selectList.push(otherImportImage[i].url);
					// }
				}
				
				console.log(selectList);
				
				that.downLoadMetail(selectList, type,selectbdList);
				return;
				const url = "_doc/" + new Date().getTime() + ".png";  // url为时间戳命名方式
				uni.saveImageToPhotosAlbum({
					filePath: url,
					success: function(res) {
						selectbdList.push(res.path);
						uni.showToast({
							title: '图片保存成功',
							icon: 'none'
						})
						bitmap.clear()
						//保存其他图片
						var otherImportImage = this.imageUrl;
						for (var i = 0; i < otherImportImage.length; i++) {
							selectList.push(otherImportImage[i].url);
							// if (otherImportImage[i]['isSelectTrue']) {
							// 	selectList.push(otherImportImage[i].url);
							// }
						}
						
						console.log(selectList);
						
						that.downLoadMetail(selectList, type,selectbdList);
						
					}
				});
				return;
				// if (this.imageUrl == '') {
				// 	this.queryPoster();
				// 	uni.showToast({
				// 		title: '分享图片生成失败 请重试',
				// 		icon: 'none'
				// 	});
				// } else {
					
					let base64 = this.imageUrl;
					const bitmap = new plus.nativeObj.Bitmap("test");
					bitmap.loadBase64Data(base64, function() {
						const url = "_doc/" + new Date().getTime() + ".png";  // url为时间戳命名方式
						console.log('saveHeadImgFile', url)
						bitmap.save(url, {
							overwrite: true,  // 是否覆盖
							// quality: 'quality'  // 图片清晰度
						}, (i) => {
							uni.saveImageToPhotosAlbum({
								filePath: url,
								success: function(res) {
									selectbdList.push(res.path);
									uni.showToast({
										title: '图片保存成功',
										icon: 'none'
									})
									bitmap.clear()
									//保存其他图片
									var otherImportImage = this.imageUrl;
									for (var i = 0; i < otherImportImage.length; i++) {
										if (otherImportImage[i]['isSelectTrue']) {
											selectList.push(otherImportImage[i].url);
										}
									}
									
									console.log(selectList);
									
									that.downLoadMetail(selectList, type,selectbdList);
									
								}
							});
						}, (e) => {
							uni.showToast({
								title: '图片保存失败',
								icon: 'none'
							})
							bitmap.clear()
						});
					}, (e) => {
						uni.showToast({
							title: '图片保存失败!',
							icon: 'none'
						})
						bitmap.clear()
					});
					

					
				//}
			},
			downLoadMetail(imageList, type,selectbdList) {
				uni.showLoading({
					title: '保存中...',
					mask: true
				});
				var that = this;
				// 批量保存选中的图片
				var saveNum = 0;
				if(imageList.length>0){
					for (var i = 0; i < imageList.length; i++) {
						saveNum++;					
						var imgUrl = imageList[i];
						uni.saveImageToPhotosAlbum({
							filePath: imgUrl,
							success: function(res) {
								console.log(res)
								selectbdList.push(res.path);
								if (saveNum == imageList.length) {
									uni.hideLoading();
									uni.showToast({
										title: '保存成功',
										icon: 'none'
									});
									if (type == 1) {
										plus.share.sendWithSystem({
											pictures: selectbdList
										}, function() {
											console.log('分享成功');
										}, function(e) {
											console.log('分享失败：' + JSON.stringify(e));
										});
									}
								}
							}
					
						});
					}
				}else{
					uni.hideLoading();
					uni.showToast({
						title: '保存成功',
						icon: 'none'
					});
					if (type == 1) {
						plus.share.sendWithSystem({
							pictures: selectbdList
						}, function() {
							console.log('分享成功');
						}, function(e) {
							console.log('分享失败：' + JSON.stringify(e));
						});
					}
				}
				
			},
			previewImage() {
				uni.previewImage({
					current: this.imageUrl,
					urls: [this.imageUrl]
				});
			},
			catchTrill() {
				var h5 = this.goodsinfo.h5Url;
                if(h5){
    				helper.setClipboardData(h5, r => {
    					uni.showToast({
    						title: '推广链接复制成功',
    						icon: 'none'
    					});
    				});
                }else{
                    this.copyWord();
                }
			},
			quanxuan(){
				this.isquanxuan=1;
				for (var i = 0; i < this.otherImportImage.length; i++) {
					this.otherImportImage[i].isSelectTrue=true;
				}
			},
			quxiao(){
				this.isquanxuan=0;
				for (var i = 0; i < this.otherImportImage.length; i++) {
					this.otherImportImage[i].isSelectTrue=false;
				}
			},
			catchTapAllPic(item, index) {
				this.$set(item, 'isSelectTrue', !item.isSelectTrue);
			},
			onLoadGoods(goods) {
				var otherImportImage = [];
				for (var i = 0; i < goods['small_images'].length; i++) {
					otherImportImage.push({
						url: goods['small_images'][i],
						isSelectTrue: false
					});
				}
				var goods_yprice = goods.goods_yprice.toString();
				var goods_ypriceArr = goods_yprice.split('.');
				this.goodsinfo = goods;
				this.otherImportImage = otherImportImage;
				this.goodsinfo.goods_ypriceArr = goods_ypriceArr;
				this.queryPoster();
				this.copyWord();
			},
			async queryPoster() {
				var goodsinfo = this.goodsinfo;
				console.log(goodsinfo);
				//return false;
				var post = {
					uid: this.$store.state.openid,
					goodsid: goodsinfo.goods_numiid,
					title: goodsinfo.goods_title,
					user_icon: this.userinfo.headimg,
					user_name: this.userinfo.nickname,
					goods_price: goodsinfo.goods_price,
					goods_yprice: goodsinfo.goods_yprice,
					goods_sale: goodsinfo.goods_sale,
					shop_name: goodsinfo.shop_name,
					shopIcon: goodsinfo.shopIcon,
					goods_picture: goodsinfo.goods_picture,
					coupon_money: goodsinfo.coupon_money,
					goods_type: goodsinfo.goods_type,
					dy_password: goodsinfo.dy_password,
					text: goodsinfo.h5Url,
					user_name_after: '为你推荐',
				};
				console.log(post)
				const res = await getApp().globalData.request.post('poster/createGoodsPoster', post);
				//console.log(res);
				if (res.code == 200) {
					this.imageUrl = res.data.imageUrl;
					//console.log(this.imageUrl);
				}
			},
			copyWord() {
				helper.setClipboardData(this.detail.content, r => {
					uni.showToast({
						title: '文案复制成功'
					});
				});
			},
			copyTjy() {
				helper.setClipboardData(this.goodsinfo.goods_info, r => {
					uni.showToast({
						title: '推荐语复制成功'
					});
				});
			},
			sharetimeline() {
				var selectList = [];var imageUrl=this.imageUrl;
				//if(this.imageUrl && this.imageUrl.substr(0,4)=='http'){
				// if(this.imageUrl){
				//     selectList.push(this.imageUrl);imageUrl=this.imageUrl;
				// }
				var that=this;
				//var otherImportImage = this.imageUrl;
				//for (var i = 0; i < otherImportImage.length; i++) {
				//	selectList.push(otherImportImage[i].url);
					// if (otherImportImage[i]['isSelectTrue']) {
					// 	selectList.push(otherImportImage[i].url);
					// }
				//}
				//console.log(selectList);
                // if(selectList.length==0){
                //     imageUrl=otherImportImage[0].url;
                //     selectList.push(imageUrl);
                // }
				if (imageUrl.length <= 1) {console.log(imageUrl);
				console.log(1212);
					that.copyWord();
					uni.share({
						provider: "weixin",
						scene: "WXSceneTimeline",
						type: 2,
						imageUrl:imageUrl[0].url,
						success: function (res) {
							uni.showToast({
								title: '分享成功'
							});
						},
						fail: function (err) {
							console.log(err);
							uni.showToast({
								title: '分享失败!'
							});
						}
					});
				}else{
					uni.showModal({
						title: '提示',
						content: '因朋友圈不支持多图分享,系统将自动复制文案,并将素材保存到相册,请手动选取发送',
						cancelText:'取消',
						confirmText:'打开微信去分享',
						success: function (res) {
							if (res.confirm) {
								that.copyWord();
								that.executeDownload(0);
								that.launchApp({
									name: '微信',
									pname: 'com.tencent.mm',
									scheme: 'weixin://'
								})
							} else if (res.cancel) {
								
							}
						}
					});
				}
				
			},
			launchApp(d) {
				//#ifndef APP-PLUS
				return false;
				//#endif
				let _this = this;
				// 打开第三方引用，ios 、 安卓有一些参数不一致，需要区分，详情：http://www.html5plus.org/doc/zh_cn/runtime.html#plus.runtime.launchApplication
				// 判断应用是否安装
				if (
					plus.runtime.isApplicationExist({
						pname: d.pname,
						action: d.scheme
					})
				) {
					// 判断平台
					if (plus.os.name == 'Android') {
						plus.runtime.launchApplication({
								pname: d.pname,
								action: d.scheme
							},
							function(e) {
								console.log('Open system default browser failed: ' + e.message);
							}
						);
					} else if (plus.os.name == 'iOS') {
						plus.runtime.launchApplication({
								action: d.scheme
							},
							function(e) {
								console.log('Open system default browser failed: ' + e.message);
							}
						);
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '程序未安装，请安装后重试',
						success: function(res) {
							if (res.confirm) {
								console.log('用户点击确定');
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}
			}
		}
	};
</script>

<style>
	.share_profits {
		background-color: #f2f2f2;
		flex: 1;
		flex-direction: column;
	}

	.list {
		flex: 1;
		flex-direction: column;
		width: 750rpx;
	}

	.tkrates {
		margin: 20rpx;
	}

	.tkrates-box {
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 20rpx;
	}

	.tkrates-text {
		font-size: 28rpx;
		font-weight: 900;
		color: #ff0000;
	}

	.tkrates-body {
		width: 660rpx;
		height: 60rpx;
		justify-content: space-between;
	}


	.tkrates-title {
		font-size: 30rpx;
		font-weight: 900;
		color: #000;
	}

	.tkrates-copy {
		font-size: 23rpx;
		background-color: #f4e4e5;
		color: #ff0000;
		padding: 0 20rpx;
		line-height: 40rpx;
		height: 40rpx;
		border-radius: 30rpx;
	}

	.rates-icon {
		font-size: 35rpx;
		color: #ff0000;
	}

	.scroll-help-view {
		margin: 20rpx;
		margin-top: 0rpx;
	}

	.scroll-view-mater {
		margin: 20rpx;
		margin-top: 0rpx;
	}

	.matter-else-text {
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 20rpx;
	}

	.matter-else-text-wenan {
		font-family: PingFangSC-Regular;
		font-size: 26rpx;
		color: #666;
	}

	.eat-row {
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 20rpx;
	}

	.left-poster {
		width: 236rpx;
		height: 400rpx;
	}

	.imageUrl {
		flex: 1;
		flex-direction: column;
	}

	.eat-small {}

	.eat-ok {
		width: 180rpx;
		height: 180rpx;
		margin-left: 20rpx;
		margin-bottom: 20rpx;
		float: left;
	}

	.eat-small-img {
		flex: 1;
		width: 100px;
		height: 100px;
		
	}

	.sel-img {
		width: 40rpx;
		height: 40rpx;
		position: absolute;
		right: 12rpx;
		top: 17rpx;
	}

	.new-share-view {
		width: 750rpx;
		height: 204rpx;
			position: fixed;
	left: 0rpx;
	bottom: 0rpx;
		background-color: #fff;
	}

	.bottom-share-view {
		padding: 20rpx;
		padding-left: 50rpx;
		padding-right: 50rpx;
	}

	.new-share-item {
			flex-direction: column;
	align-items: center;
	}

	.new-share-item-image {
		width: 100rpx;
		height: 100rpx;
	}

	.new-share-item-text {
		font-size: 24rpx;
		color: #7c8187;
		margin-top: 14rpx;
		text-align: center;
	}
	.s-nowrap{flex-direction:row; flex-wrap:nowrap;}/*flex  横向不换行*/
	.s-space-between{flex-direction:row; justify-content:space-between;}/*flex 横向两端对齐*/
</style>
